package ar.com.hakasoft

import ar.com.hakasoft.entities.Egreso
import ar.com.hakasoft.utils.DateUtil

class EgresoController extends LoginController {

	def egresosService
	
	def beforeInterceptor = [action:this.&checkUser,except:[]]
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
		def egresoInstance = new Egreso(username: session?.user?.username)
		egresoInstance.properties = params
		return [egresoInstance: egresoInstance, listadoEgresos : []]
    }
	
	def agregarEgreso = {
		log.warn params
		def egresoInstance = new Egreso(params)
		
		if (params.fechaHidden){
			egresoInstance.fecha = obtenerFecha(params.fechaHidden)
		}
		
		egresoInstance.total =  new BigDecimal (params.total)
		egresosService.guardarEgreso(egresoInstance)
		
		def listadoEgresos = egresosService.obtenerEgresoPorFecha(params.fechaHidden.replace("/", " "))
		render(template: "/templates/tablaItemsEgresos", model: [listadoEgresos: listadoEgresos])
//		if (egresoInstance.save(flush: true, failOnError: true)) {
//			flash.message = "${message(code: 'default.created.message', args: [message(code: 'egreso.label', default: 'Egreso'), egresoInstance.id])}"
//			redirect(action: show, id: egresoInstance.id)
//		}
//		else {
//		}
	}
	
	def eliminarEgreso = {
		def egresoInstance = Egreso.get(params.id)
		def fecha = egresoInstance.fecha
		if (egresoInstance) {
			try {
				egresoInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
		}
		def listadoEgresos = egresosService.obtenerEgresoPorFecha(DateUtil.toString(fecha).replaceAll("/", " "))
		render(template: "/templates/tablaItemsEgresos", model: [listadoEgresos: listadoEgresos])
	}
	
	def finalizar = {
		if (params.fechaHidden){
			redirect(action: show, params:[fecha: params.fechaHidden])
		}
	}
	
	def nuevoEgreso = {
		redirect(action: index)
	}
	
	def editar = {
		def egresoInstance = Egreso.get(params.id)
		render (view: "index", model: [egresoInstance: egresoInstance])
	}
	
	def show = {
		log.warn params
		putParams(params)
	}
	
	def imprimir = {
		log.warn "entro al imprimir egreso"
		putParams(params)
	}
	
	def putParams(params){
		log.warn "parametros egreso: " + params
		def listadoEgresos = egresosService.obtenerEgresoPorFecha(params.fecha?.replace("/", " "))
		//def egresoInstance = Egreso.get(params.fecha)
		log.warn "listadoEgresos: ${listadoEgresos}"
		if (!listadoEgresos) {
			log.warn "no hay egresos"
			flash.message = "No se encontraron egresos"
			redirect(action: "index")
		}
		else {
			log.warn "entro en el else"
			flash.message = ""
			[listadoEgresos: listadoEgresos]
		}
	}
	/*    
	def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [egresoInstanceList: Egreso.list(params), egresoInstanceTotal: Egreso.count()]
    }

    def create = {
        def egresoInstance = new Egreso(username: session?.user?.username)
        egresoInstance.properties = params
        return [egresoInstance: egresoInstance]
    }

    def save = {
        def egresoInstance = new Egreso(params)
		egresoInstance.properties = params
        if (egresoInstance.save(flush: true, failOnError: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'egreso.label', default: 'Egreso'), egresoInstance.id])}"
            redirect(action: "show", id: egresoInstance.id)
        }
        else {
            render(view: "create", model: [egresoInstance: egresoInstance])
        }
    }


    def edit = {
        def egresoInstance = Egreso.get(params.id)
        if (!egresoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [egresoInstance: egresoInstance]
        }
    }

    def update = {
        def egresoInstance = Egreso.get(params.id)
        if (egresoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (egresoInstance.version > version) {
                    
                    egresoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'egreso.label', default: 'Egreso')] as Object[], "Another user has updated this Egreso while you were editing")
                    render(view: "edit", model: [egresoInstance: egresoInstance])
                    return
                }
            }
            egresoInstance.properties = params
            if (!egresoInstance.hasErrors() && egresoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'egreso.label', default: 'Egreso'), egresoInstance.id])}"
                redirect(action: "show", id: egresoInstance.id)
            }
            else {
                render(view: "edit", model: [egresoInstance: egresoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def egresoInstance = Egreso.get(params.id)
        if (egresoInstance) {
            try {
                egresoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'egreso.label', default: 'Egreso'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def guardar = {
		def egresoInstance
		if (params.id){
			egresoInstance = Egreso.findById(params.id)
		}else{
			egresoInstance = new Egreso(params)
		}
		
		if (params.inputDate){
			egresoInstance.fecha = obtenerFecha(params.inputDate)
		}
		egresoInstance.total =  new BigDecimal (params.total)
		
		log.warn params.id
		log.warn "${egresoInstance.id}"
		
		if (egresoInstance.save(flush: true, failOnError: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'egreso.label', default: 'Egreso'), egresoInstance.id])}"
			redirect(action: show, id: egresoInstance.id)
		}
		else {
			render(view: "index", model: [egresoInstance: egresoInstance])
		}
	}*/
	
}
